package com.matt.service.impl;

import com.matt.domain.record.BirthdayRecord;
import com.matt.domain.Children;
import com.matt.domain.rel.RelChildrenBirthdayRecord;
import com.matt.foundation.constant.ChildrenConstant;
import com.matt.foundation.constant.FeedsConstant;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.query.BirthdayRecordQuery;
import com.matt.foundation.utils.annotation.OptsTrace;
import com.matt.foundation.vo.BirthdayRecordPageVo;
import com.matt.repository.record.BirthdayRecordRepository;
import com.matt.repository.ChildrenRepository;
import com.matt.repository.base.ChildrenBirthdayRecordRepository;
import com.matt.repository.rel.RelChildrenBirthdayRecordRepository;
import com.matt.service.BirthdayRecordService;
import com.matt.service.FeedsService;
import com.matt.service.remoteCall.SsoCallService;
import com.matt.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/22
 * *******************************
 */
@Service
@Transactional
public class BirthdayRecordServiceImpl implements BirthdayRecordService{

    @Autowired
    private RelChildrenBirthdayRecordRepository relChildrenBirthdayRecordRepository;

    @Autowired
    private BirthdayRecordRepository birthdayRecordRepository;

    @Autowired
    private ChildrenBirthdayRecordRepository childrenBirthdayRecordRepository;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private ChildrenRepository childrenRepository;

    @Autowired
    private FeedsService feedsService;


    @OptsTrace(code = "0005")
    @Override
    public BirthdayRecord addBirthdayRecord(String staffId, String areaId, String title, String content, MultipartFile[] files, Integer type) {

        String resourceUrl = uploadService.uploadLiveRecordImg(staffId, type, files);
        StaffDTO staff = ssoCallService.getStaffDTO(null, staffId);
        BirthdayRecord birthdayRecord = BirthdayRecord.of(title, content, type, resourceUrl, "", staffId, staff.getRealName());
        birthdayRecordRepository.save(birthdayRecord);
        List<Children> childrenList = childrenRepository.findByAreaIdAndStatus(areaId, ChildrenConstant.ChildrenStatus.ACTIVE.codeOf());

        for (Children children : childrenList) {
            RelChildrenBirthdayRecord relChildrenBirthdayRecord = new RelChildrenBirthdayRecord();
            relChildrenBirthdayRecord.setBirthdayRecordId(birthdayRecord.getId());
            relChildrenBirthdayRecord.setChildrenId(children.getId());
            relChildrenBirthdayRecordRepository.save(relChildrenBirthdayRecord);

            //存储动态
            if (type == 2)
                feedsService.save(staffId, children.getId(), areaId, "", FeedsConstant.BIRTHDAY_MEETING, title, content,"", resourceUrl, birthdayRecord.getId());
            else if (type == 1)
                feedsService.save(staffId, children.getId(), areaId, "", FeedsConstant.BIRTHDAY_MEETING, title, content, resourceUrl, "", birthdayRecord.getId());
            else
                feedsService.save(staffId, children.getId(), areaId, "", FeedsConstant.BIRTHDAY_MEETING, title, content, "","", birthdayRecord.getId());

        }
        return birthdayRecord;
    }

    @Override
    public Page<BirthdayRecordPageVo> getBirthdayRecordByChildren(BirthdayRecordQuery query) {
        List<Object[]> objects = childrenBirthdayRecordRepository.findRecordsByChildren(query);
        List<BirthdayRecordPageVo> list = objects.stream().map(o->BirthdayRecordPageVo.of(o)).collect(Collectors.toList());
        Long count = childrenBirthdayRecordRepository.countRecordsByChildren(query);
        return  new PageImpl<BirthdayRecordPageVo>(list, PageRequest.of(query.getPageNum(), query.getPageSize()), count);
    }
}
